<?php
/**
 * 好友分组
 * 
 * @author 	jiaowei<jiaowei@178.com>
 * @version $Id: group.php 10843 2010-09-15 10:58:12Z jiaowei $
 */
class friends_model_group extends Model
{
	const PAGE_SIZE = 20;

	public function __construct()
	{
		parent::__construct();
		$this->table 			  = 'cartoon_friends_group';
		$this->friend_table 	  = 'cartoon_friends';  //好友关系表
		$this->group_friend_table = 'cartoon_friends_group_relate';
	}
	
	/**
	 * 添加好友分组
	 *
	 * @param array $data
	 * @return int
	 */
	public function addGroup($data)
	{
		return $this->db->insert($this->table, $data);
	}
	
	/**
	 * 添加多个分组
	 *
	 * @param int $uid
	 * @param array $groups
	 * @return int
	 */
	public function addGroups($uid, $groups)
	{
		$str = '';
		foreach ((array)$groups AS $v) {
			$str .= "('{$uid}', '{$v}'),";
		}
		$str = trim($str, ',');
		$sql = "INSERT INTO " . $this->table . " (`uid`, `group_name`) VALUES {$str}";
		return $this->db->query($sql);
	}
	
	/**
	 * 编辑分组信息
	 *
	 * @param array $data
	 * @param int $gid
	 * @return bool
	 */
	public function editGroup($data, $gid)
	{
		return $this->db->update($this->table, $data, array('group_id' => $gid), 'group_id=&group_id');
	}
	
	/**
	 * 删除好友分组
	 *
	 * @param int $hostId
	 * @param int $gid
	 * @return int
	 */
	public function delGroup($hostId, $gid)
	{
		return $this->db->delete($this->table, $params = array('uid' => $hostId, 'group_id' => $gid));
	}
	
	/**
	 * 修改分组好友总数
	 *
	 * @param int|array $gid
	 * @param bool $status
	 * @return int
	 */
	public function editGroupFriendsNums($gid, $status = false)
	{
		$where = "WHERE `group_id` = '{$gid}'";
		
		if (is_array($gid)) {
			if (empty($gid)) {
				return false;
			}
			$gids = implode(',', $gid);
			$where = "WHERE `group_id` IN ({$gids})";
		}
		
		if ($status) {
			$sql = "UPDATE " . $this->table . " SET `group_nums` = group_nums + 1 {$where}";
		} else {
			$sql = "UPDATE " . $this->table . " SET `group_nums` = if(group_nums > 1, group_nums -1 , 0) {$where}";
		}
		return $this->db->query($sql);
	}
	
	/**
	 * 查询单个分组信息
	 *
	 * @param int $gid
	 * @return array
	 */
	public function getGroup($gid)
	{
		$sql = "SELECT * FROM " . $this->table . " WHERE `group_id` = &gid";
		return $this->db->fetchRow($sql, array('gid' => $gid));
	}
	
	/**
	 * 获取分类信息
	 *
	 * @param int $hostId
	 * @param str $groupName
	 * @return array
	 */
	public function getGroupByName($hostId, $groupName, $gid = 0)
	{
		$sql = "SELECT * FROM " . $this->table . " WHERE `uid`=&uid AND `group_name` = &gname" . ($gid ? " AND `group_id` != '{$gid}'" : '');
		return $this->db->fetchRow($sql, array('uid' => $hostId, 'gname' => $groupName));
	}
	
	/**
	 * 获取用户的所有分组
	 *
	 * @param int $uid
	 * @return array
	 */
	public function getAllGroup($uid)
	{
		$sql = "SELECT * FROM " . $this->table . " WHERE `uid` = &uid";
		return $this->db->fetchAll($sql, array('uid' => $uid));
	}
	
	/**
	 * 添加好友到某个分组
	 *
	 * @param int $hostId
	 * @param int $friendId
	 * @param int $groups
	 */
	public function addFriendToGroup($hostId, $friendId, $groups)
	{
		$str = '';
		foreach ($groups AS $v) {
			$str .= "('{$hostId}', '{$v}', {$friendId}),";
		}
		$str = trim($str, ',');
		$sql = "INSERT ignore INTO " . $this->group_friend_table . " (`host_id`, `group_id`, `friend_id`) VALUES {$str}";
		return $this->db->query($sql);
	}
	
	/**
	 * 删除好友分组关系
	 *
	 * @param int $hostId
	 * @param int $friendId
	 * @param int $gid 0: 删除所有用户的分组
	 * @return int
	 */
	public function delFriendFromGroup($hostId, $friendId = 0, $gid = 0)
	{
		if (!$hostId || ((0 == $friendId) && (0 == $gid))) {
			return false;
		}
		$params = array();
		
		//删除所有分组下的好友关系
		if (0 != $friendId) {
			$params = array('host_id' => $hostId, 'friend_id' => $friendId);
		}
		
		//删除一个分组下的所有好友
		if (0 != $gid) {
			$params = array('host_id' => $hostId, 'group_id' => $gid);
		}
		
		//删除一个分组下的一个好友
		if ($gid && $friendId) {
			$params = array('host_id' => $hostId, 'group_id' => $gid, 'friend_id' => $friendId);
		}
		
		if (!$params) {
			return false;
		}
		return $this->db->delete($this->group_friend_table, $params);
	}
	
	/**
	 * 获取好友所属的所有分组
	 *
	 * @param int $hostId
	 * @param int $friendId
	 * @return array
	 */
	public function getFriendGroup($hostId, $friendId)
	{
		$sql = "SELECT `group_id`,`friend_id` FROM " . $this->group_friend_table . " WHERE `host_id`=&host_id AND `friend_id`=&friend_id";
		return $this->db->fetchAll($sql, array('host_id' => $hostId, 'friend_id' => $friendId));
	}
	
	/**
	 * 获取该分组下的所有好友 (废弃函数)
	 *
	 * @param int $hostId
	 * @param int $gid
	 * @return array
	 */
	public function getGroupFriends($hostId, $gid, $page = 0, $pageSize = 0)
	{
		$sql = "SELECT `friend_id` FROM " . $this->group_friend_table . " WHERE `host_id`=&host_id AND `group_id`=&group_id ORDER BY `friend_id` DESC";
		if ((0 == $page) && (0 == $pageSize)) {
			$result = $this->db->fetchAll($sql, array('host_id' => $hostId, 'group_id' => $gid));
		} else {
			$result = $this->db->selectPage($sql, array('host_id' => $hostId, 'group_id' => $gid), $page, $pageSize);
			$info   = $result['INFO'];
			$result = $result['ROWS'];
		}
		if (!$result) {
			return false;
		}

		$friendsIds = '';
		foreach ($result AS $v) {
			$friendsIds .= $v['friend_id'] . ',';
		}
		$friendsIds = trim($friendsIds, ',');
		
		$sql = "SELECT `uid`, `username`, `remark` FROM " . $this->friend_table . " WHERE `host_id`=&host_id AND `uid` IN ({$friendsIds})";
		$ret = $this->db->fetchAll($sql, array('host_id' => $hostId));
		
		if ((0 == $page) && (0 == $pageSize)) {
			return $ret;
		} else {
			return array('INFO' => $info, 'ROWS' => $ret);
		}
	}
	
	/**
	 * 获取所有好友和未分组好友 (废弃函数)
	 *
	 * @param int $hostId
	 * @param int $page
	 * @param int $pageSize
	 * @param bool $status true:未分组好友  false:全部好友
	 * @return array
	 */
	public function getFriendsByPage($hostId, $page, $pageSize, $status = false)
	{
		$where = '';
		if (false !== $status) {
			$where = "AND `group_status` = 0";
		}
		$sql = "SELECT `uid`,`username`, `remark` FROM " . $this->friend_table . " WHERE `host_id` = &host_id AND `is_friend`=1 {$where} ORDER BY remark DESC";
		$params = array('host_id' => $hostId);
		return $this->db->selectPage($sql, $params, $page, $pageSize);
	}
	
	/**
	 * 获取已经分组的好友id
	 *
	 * @param int $hostId
	 */
	public function getGroupedFriends($hostId)
	{
		$friends = $this->db->fetchAll("SELECT `friend_id` FROM " . $this->group_friend_table . " WHERE `host_id` = &host_id", array('host_id' => $hostId));
		$friendIds = array();
		foreach ($friends as $v) {
			$friendIds[] = $v['friend_id'];
		}
		return array_unique($friendIds);
	}
	
	/**
	 * 好友从属关系检测
	 *
	 * @param int $hostId
	 * @param int $groupId
	 * @param int $friendId
	 * @return int
	 */
	public function checkFriendGroup($hostId, $groupId, $friendId)
	{
		$sql = "SELECT `friend_id` FROM " . $this->group_friend_table . " WHERE `host_id`=&host_id AND `group_id`=&group_id AND `friend_id`=&friend_id";
		return $this->db->fetchField($sql, 'friend_id', array('host_id' => $hostId, 'group_id' => $groupId, 'friend_id' => $friendId));
	}
	
	/**
	 * 我和 '我的好友的好友' 的共同好友
	 *
	 * @param int $uid
	 */
	public function commfriendsOfMyFriends($uid) 
	{
		$uid = (int) $uid;
		$myFriendId = friends_util_cache::getFriendIds($uid);	//我的好友列表
		if (!$myFriendId) return false;
		
		//我的好友的好友ID
		$sql = "SELECT `uid` FROM {$this->friend_table} WHERE `host_id` IN(".implode(',', (array) $myFriendId).")";
		$friendId = $this->db->fetchAll($sql);
		
		$result = array();
		$myFriendId[] = $uid;
		
		foreach ($friendId as $k => $v) {
			if (in_array($v['uid'], $myFriendId)) continue;
			
			if ($result[$v['uid']])
				++$result[$v['uid']];
			else
				$result[$v['uid']] = 1;
		}
		
		arsort($result);
		
		return $result;
	}
	
	/**
	 * 我和 '我的好友的好友' 的共同好友	(废弃函数)
	 *
	 */
	public function getUserInfo($users)
	{
		if (!$users) return false;
		
		$uids  = array();
		foreach ($users as $k => $v)
			$uids[] = $v['uid'];
			
		$allUsers = friends_util_cache::getUserInfo($uids);
		
		$tem = array();
		foreach ($allUsers as $v) {
			$tem[$v['id']] = array('name' => $v['username'], 'province' => $v['r_province']);
		}
		
		foreach ($users as $k => $v) {
			$users[$k]['r_province'] = $tem[$v['uid']]['r_province'];
			$users[$k]['username']	 = $tem[$v['uid']]['username'];
		}
		
		return $users;
	}
	
	/**
	 * 精确搜索用户
	 *
	 * @param int $uid
	 * @param str $username
	 * @return array
	 */
	public function searchUsers($uid = '', $username = '')
	{
		if (!$uid && !$username) {
			return false;
		}
		
		$where  = " WHERE 1 ";
		$params = array();
		
		if ($uid) {
			$where .= " AND id = &uid";
			$params['uid'] = $uid;
		}
		
		if ($username) {
			$where .= " AND `username` = &username ";
			$params['username'] = $username;
		}
		
		$sql = "SELECT * FROM `prop_standard` {$where}";
		return $this->db->fetchAll($sql, $params);
	}
	
	
	
	
	

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 分组下好友id列表
	 *
	 * @param int $hostId
	 * @param int $groupId
	 * @return array
	 */
	public function getListId($hostId, $groupId)
	{
		$listId = $this->db->fetchAll("select friend_id from " . $this->group_friend_table . " where host_id=&host_id and group_id =&group_id", array('host_id' => $hostId, 'group_id' => $groupId));
		
		$result = array();
		foreach ($listId as $v) {
			$result[] = $v['friend_id'];
		}
		return $result;
	}
	
	/**
	 * 获取用户分组id列表
	 *
	 * @param unknown_type $uid
	 * @return unknown
	 */
	public function getGroupListId($uid)
	{
		$listId = $this->db->fetchAll("select group_id from " . $this->table . " where uid=&uid", array('uid' => $uid));
		
		$result = array();
		foreach ($listId as $v) {
			$result[] = $v['group_id'];
		}
		return $result;
	}
	
	/**
	 * 有分组id获取分组信息
	 *
	 * @param array $ids
	 * @return array
	 */
	public function getGroupRow($ids)
	{
		$ids = (array) $ids;
		$ids = implode(',', $ids);
		return $this->db->fetchAll("SELECT group_id as id, group_id, uid, group_name, group_nums FROM " . $this->table . " WHERE group_id IN({$ids})");
	}
}
